package com.dd.dus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.dc.entity.users.*;
import com.dd.dus.mapper.*;
import com.dd.dus.service.IDUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-03-12
 */
@Service
public class DUserServiceImpl extends ServiceImpl<DUserMapper, DUser> implements IDUserService {

    @Autowired
    DUserMapper userMapper;

    @Autowired
    DPermissionMapper permissionMapper;

    @Autowired
    DRoleMapper roleMapper;

    @Autowired
    DUserRoleRelationMapper userRoleRelationMapper;

    @Autowired
    DRolePermissionRelationMapper rolePermissionRelationMapper;

    @Override
    public Page<DUser> queryUserList(int page,int row){
        Page<DUser> userIPage = new Page<>(page, row);
        userIPage = userMapper.selectPage(userIPage, null);
        userIPage.setTotal(userIPage.getRecords().size());
        return userIPage;
    }

    @Override
    public DUser queryUserOne(DUser user){
        QueryWrapper wrapper = new QueryWrapper(user);
        DUser dUser = userMapper.selectOne(wrapper);
        return dUser;
    }

    @Override
    public DUser queryUserId(Integer userId){
        DUser dUser = userMapper.selectById(userId);
        return dUser;
    }

    @Override
    public Integer insert(DUser user){
        userMapper.insert(user);
        return user.getUserId();
    }

    @Override
    public Integer update(DUser user){
        return userMapper.update(user, new UpdateWrapper<>(user));
    }

    @Override
    public DUser queryUserInfo(Integer userId){
        // 根据用户编号查询角色编号
        DUserRoleRelation userRoleRelation = new DUserRoleRelation().setUserId(userId);
        AtomicReference<LambdaQueryWrapper> queryWrapper = new AtomicReference<>(new LambdaQueryWrapper(userRoleRelation));
        List<DUserRoleRelation> userRoleRelations = userRoleRelationMapper.selectList(queryWrapper.get());
        // 根据角色编号查询角色
        List<DRole> dRoles = new ArrayList<>();
        // 根据角色编号查询权限List
        List<DPermission> dPermissions = new ArrayList<>();
        Optional.ofNullable(userRoleRelations).orElse(new ArrayList<>()).forEach(urRelation -> {
            // 角色
            DRole dRole = new DRole();
            dRole.setRoleId(urRelation.getRoleId());
            queryWrapper.set(new LambdaQueryWrapper(dRole));
            dRole = roleMapper.selectOne(queryWrapper.get());
            dRoles.add(dRole);
            // 权限菜单
            DRolePermissionRelation rolePermissionRelation = new DRolePermissionRelation().setRoleId(urRelation.getRoleId());
            queryWrapper.set(new LambdaQueryWrapper(rolePermissionRelation));
            List<DRolePermissionRelation> rolePermissionRelations = rolePermissionRelationMapper.selectList(queryWrapper.get());
            List<Integer> perIds = rolePermissionRelations.stream().map(rp -> rp.getPId()).collect(Collectors.toList());
            dPermissions.addAll(permissionMapper.selectBatchIds(perIds));
        });
        // 去除重复
        dPermissions.stream().distinct().collect(Collectors.toList());
        DUser dUser = DUser.builder().userId(userId).roles(dRoles).permissions(dPermissions).build();
        return dUser;
    }

}
